package com.emmy.walnut.presenter;

import android.os.Handler;
import android.os.Message;

import com.emmy.walnut.app.AppDatabase;
import com.emmy.walnut.constant.AppConstant;
import com.emmy.walnut.model.Record;
import com.emmy.walnut.model.TimingObject;
import com.emmy.walnut.presenter.iface.ActivityPIface;
import com.emmy.walnut.view.iface.ActivityFraIface;

public class ActivityPresenter implements ActivityPIface {

    private ActivityFraIface activityFraIface;

    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what){
                case AppConstant.START_RECORD:
                    activityFraIface.startRecords();
                    break;
            }
            return false;
        }
    });

    public ActivityPresenter(ActivityFraIface activityFraIface) {
        this.activityFraIface = activityFraIface;
    }

    @Override
    public void startRecord(final Record record) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                long id = AppDatabase.getInstance().recordDao().insert(record);
                record.setId(id);
                AppConstant.undoneRecords.add(record);
                Message.obtain(handler, AppConstant.START_RECORD).sendToTarget();
            }
        }).start();
//        JSONObject jsonObject = new JSONObject();
//        String timestamp = String.valueOf(System.currentTimeMillis());
//        String token = MD5Util.md5(AppConstant.uuid + AppConstant.accessKey + timestamp + AppConstant.SALT_VALUE);
//        try {
//            jsonObject.put("end_time", timingObject.getEndTime()/1000*1000);
//            jsonObject.put("end_tag", false);
//            jsonObject.put("start_time", timingObject.getStartTime()/1000*1000);
//            jsonObject.put("label", timingObject.getActivityName());
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//        RequestBody requestBody = RequestBody.create(MediaType.get("application/json"), jsonObject.toString());
//        RetrofitHelper.getInstance().serviceApi().addTimeZone(AppConstant.accessKey, timestamp, token,
//                AppConstant.uuid, requestBody).
//                subscribeOn(Schedulers.io()).
//                observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<GeneralResult>() {
//            @Override
//            public void onCompleted() {
//
//            }
//
//            @Override
//            public void onError(Throwable e) {
//
//            }
//
//            @Override
//            public void onNext(GeneralResult addActivityResult) {
//            }
//        });
    }

    @Override
    public void endRecord(final Record record) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                AppDatabase.getInstance().recordDao().update(record);
            }
        }).start();
//        String timestamp = String.valueOf(System.currentTimeMillis()/1000*1000);
//        String token = MD5Util.md5(AppConstant.uuid + AppConstant.accessKey + timestamp + AppConstant.SALT_VALUE);
//        JsonObject jsonObject = new JsonObject();
//        jsonObject.addProperty("end_tag", true);
//        jsonObject.addProperty("end_time", timestamp);
//        jsonObject.addProperty("label", timingObject.getActivityName());
//        jsonObject.addProperty("start_time", timingObject.getStartTime()/1000*1000);
//        RequestBody requestBody = RequestBody.create(MediaType.get(AppConstant.MEDIA_JSON_TYPE),
//                jsonObject.toString());
//        RetrofitHelper.getInstance().serviceApi().modifyTimeZone(AppConstant.accessKey, timestamp, token,
//                AppConstant.uuid, id, requestBody).subscribeOn(Schedulers.io()).
//                observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BaseResult>() {
//            @Override
//            public void onCompleted() {
//
//            }
//
//            @Override
//            public void onError(Throwable e) {
//
//            }
//
//            @Override
//            public void onNext(BaseResult baseResult) {
//                if (baseResult.getSuccess()){
//
//                }
//            }
//        });
    }

    @Override
    public void getActivityIDAndEnd(final TimingObject timingObject) {
//        String timestamp = String.valueOf(System.currentTimeMillis());
//        String token = MD5Util.md5(AppConstant.uuid + AppConstant.accessKey + timestamp + AppConstant.SALT_VALUE);
//        RetrofitHelper.getInstance().serviceApi().getTimeZone(AppConstant.accessKey,
//                timestamp, token, AppConstant.uuid, false, timingObject.getEndTime()/1000*1000,
//                timingObject.getStartTime()/1000*1000).subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Subscriber<BaseResult>() {
//                    @Override
//                    public void onCompleted() {
//
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//
//                    }
//
//                    @Override
//                    public void onNext(BaseResult baseResult) {
//                        if (baseResult.getSuccess()) {
//                            ActivityResult activityResult = gson.fromJson(baseResult.getResult().toString(),
//                                    ActivityResult.class);
//                            endActivity(timingObject, activityResult.getList().get(0).getId());
//                        }
//                    }
//                });
    }
}
